----------------------------------------------------------------------------
--- hook chathistory 模块，实现系统悄悄话。  chathistory.lua
--- function ChatHistoryManager:GenerateChatMessage(type, sender_userid, sender_netid, sender_name, message, colour, icondata, whisper, localonly, text_filter_context)
--- 改掉其中一个参数
--- 客户端用 ChatHistory:AddToHistory 执行显示语句，该函数内部会调用 ChatHistory.GenerateChatMessage
----------------------------------------------------------------------------
-- icondata : "profileflair_treasurechest_monster" ,"default"  在文件 misc_items.lua 里，部分需要玩家解锁
-- ChatHistory:AddToHistory({flag = "npc_mod" , ChatType = ChatTypes.Message , m_colour = {0,0,255} , s_colour = {255,255,0}},nil,nil,"NPC","656565",{0,255,0})
-- m_colour 文本颜色，  s_colour 名字颜色
-- colour 参数 和官方的有出入，  需要除以255才能成任意色。
----------------------------------------------------------------------------


ChatHistory.GenerateChatMessage_old_npc_mod = ChatHistory.GenerateChatMessage
ChatHistory.GenerateChatMessage = function(self,Chat_type, sender_userid, sender_netid, sender_name, message, colour, icondata, whisper, localonly, text_filter_context)
    local npc_mod_cmd_table = {}
    if Chat_type and type(Chat_type) == "table" and Chat_type.flag == "npc_mod" then
        npc_mod_cmd_table = deepcopy(Chat_type)
        Chat_type = npc_mod_cmd_table.ChatType or ChatTypes.Message                        ---- 文本类型
        localonly = true                                                                ---- 只在本地
        icondata = npc_mod_cmd_table.icondata or icondata -- or "default"                     ---- 图标
        sender_name = npc_mod_cmd_table.sender_name or sender_name                         ---- 发送者名字
        message = npc_mod_cmd_table.message or message                                     ---- 文字内容
        colour = npc_mod_cmd_table.s_colour or npc_mod_cmd_table.m_colour or {255/255,255/255,255/255}    ---- 颜色初始化。
    end

    local ret_chat_message = self:GenerateChatMessage_old_npc_mod(Chat_type, sender_userid, sender_netid, sender_name, message, colour, icondata, whisper, localonly, text_filter_context)
    
    if npc_mod_cmd_table.flag == "npc_mod" then
        if npc_mod_cmd_table.m_colour then            
            ret_chat_message.m_colour = npc_mod_cmd_table.m_colour --- 文本颜色
        end
        if npc_mod_cmd_table.s_colour then
            ret_chat_message.s_colour = npc_mod_cmd_table.s_colour --- 文本颜色
        end
    end

    return ret_chat_message
end

AddPlayerPostInit(function(inst)
    
    if TheNet:IsDedicated() then    -- 是服务器加载到这就返回。 事件添加到 客户端足够了，用 RPC EVENT 下发
        return
    end

    inst:ListenForEvent("npc_mod_event.whisper",function(_,_table)
        if _table and _table.message then
            _table.flag = "npc_mod"
            ChatHistory:AddToHistory(_table)
        end
    end)

end)



---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---- widgets/text   偶发性的崩溃：  [string "scripts/widgets/text.lua"]:58: bad argument #3 to 'SetColour' (number expected, got no value)
---- widgets/text SetColour
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------



AddClassPostConstruct("widgets/text",function(self)
    local old_fn = self.SetColour
    self.SetColour = function(self,r,g,b,a)
        local colour = type(r) == "number" and { r, g, b, a } or r
        r,g,b,a = unpack(colour)
        old_fn(self,r or 1,g or 1,b or 1 ,a or 1)
    end
end)